package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.mapper.ApBehaviorEntryMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP行为实体表,一个行为实体可能是用户或者设备，或者其它 服务实现类
 * </p>
 *
 * @author hsg
 * @since 2022-07-06
 */
@Service
public class ApBehaviorEntryServiceImpl extends ServiceImpl<ApBehaviorEntryMapper, ApBehaviorEntry> implements IApBehaviorEntryService {

    @Override
    public ApBehaviorEntry getEntryByUserIdOrEquipmentId(Integer userId, String equipmentId) {
        LambdaQueryWrapper<ApBehaviorEntry> queryWrapper = new LambdaQueryWrapper<>();

        if(userId==0){ //未登录 ，使用设备id
            queryWrapper.eq(ApBehaviorEntry::getEntryId,equipmentId);
        }else{
            queryWrapper.eq(ApBehaviorEntry::getEntryId,userId.toString());
        }
        ApBehaviorEntry entry = this.getOne(queryWrapper);
        if(entry==null){
            // 没有实体 ，应该新增一个
            entry = new ApBehaviorEntry();
            if(userId==0){
                entry.setType(0);   //实体类型            0终端设备            1用户
                entry.setEntryId(equipmentId);
            }else{
                entry.setType(1);   //实体类型            0终端设备            1用户
                entry.setEntryId(userId.toString());
            }
            entry.setCreatedTime(new Date());
            this.save(entry);
        }
        return entry;
    }
}
